package project.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import project.dao.IPetDao;
import project.entity.Pet;

public class PetDaoImpl implements IPetDao {

	private static final List<Pet> list = new ArrayList<>();

	@Override
	public void save(Pet pet) {
		list.add(pet);
	}

	@Override
	public void delete(int id) {
		Iterator<Pet> it = list.iterator();
		while (it.hasNext()) {
			Pet pet = it.next();
			if (pet.getId() == id) {
				it.remove();
				System.out.println("编号为:" + id + "的宠物已删除");
				break;
			}
		}

	}

	@Override
	public void updatePrice(int id, double price) {
		for (Pet p : list) {
			if (p.getId() == id) {
				p.setPrice(price);
				System.out.println("编号为:" + id + "的宠物价格已修改");
			}
		}

	}

	@Override
	public List<Pet> findAll() {
		return list;
	}

	@Override
	public Pet findById(int id) {
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).getId() == id) {
				return list.get(i);
			}
		}
		return null;
	}

	public static void main(String[] args) {
		IPetDao dao = new PetDaoImpl();
		dao.save(new Pet("1", "1", "1", 1, 1.1, 2.2));
		System.out.println(dao.findAll());
	}

}
